# coding=utf-8

import os 
import shutil


def moveNoType_xml(xml_path,img_path,new_imagepath,key):
    xml_list = os.listdir(xml_path)
    print(xml_list)
    for xml_file in xml_list:
        xml_read = os.path.join(xml_path,xml_file)
        with open(xml_read,'r') as xml_obj:
            xml_text = xml_obj.readlines()
            # print(xml_text)
            if key in xml_text  :
                print(xml_file,'is ok')
            else:
                xml_oldpath = os.path.join(xml_path,xml_file)
                # xml_newpath = os.path.join(new_path,xml_file)
                # shutil.move(xml_oldpath,xml_newpath)

                img_file = xml_file.replace('.xml','.jpg')
                img_oldpath = os.path.join(img_path,img_file)
                # img_newpath = os.path.join(new_imagepath,img_file)
                # shutil.move(img_oldpath,img_newpath)
                os.remove(img_oldpath)


if __name__ == '__main__':
    class_file = ("yqsl_dianjieshui_qingning_20210226_yqsl_morning")
    path = ("/home/wangwm/Music/")
    xml_path = (path + class_file + "_xml")
    # new_path = (path + class_file +"_xml_No_type")
    img_path = (path + class_file)
    # new_imagepath = (path + class_file +"_No_type")
    # if not os .path.exists(new_path):
    #     os.makedirs(new_path)
    # if not os .path.exists(new_imagepath):
    #     os.makedirs(new_imagepath)
    moveNoType_xml((xml_path),(img_path),(new_imagepath),key = '\t<object>\n')
